package rdbms.util;


import common.exception.DataExchangeException;
import common.spi.ErrorCode;

public class RdbmsException extends DataExchangeException
{
    public RdbmsException(ErrorCode errorCode, String message)
    {
        super(errorCode, message);
    }

    public static DataExchangeException asConnException(Exception e, String userName, String dbName)
    {
        DBUtilErrorCode dbUtilErrorCode = mySqlConnectionErrorAna(e.getMessage());
        if (dbUtilErrorCode == DBUtilErrorCode.MYSQL_CONN_DB_ERROR && dbName != null)
        {
            return DataExchangeException.asDataExchangeException(dbUtilErrorCode, "该数据库名称为：" + dbName + " 具体错误信息为：" + e);
        }
        if (dbUtilErrorCode == DBUtilErrorCode.MYSQL_CONN_USERPWD_ERROR)
        {
            return DataExchangeException.asDataExchangeException(dbUtilErrorCode, "该数据库用户名为：" + userName + " 具体错误信息为：" + e);
        }
        return DataExchangeException.asDataExchangeException(dbUtilErrorCode, " 具体错误信息为：" + e.getMessage());
    }

    public static DBUtilErrorCode mySqlConnectionErrorAna(String e)
    {
        if (e.contains(Constant.MYSQL_DATABASE))
        {
            return DBUtilErrorCode.MYSQL_CONN_DB_ERROR;
        }

        if (e.contains(Constant.MYSQL_CONNEXP))
        {
            return DBUtilErrorCode.MYSQL_CONN_IPPORT_ERROR;
        }

        if (e.contains(Constant.MYSQL_ACCDENIED))
        {
            return DBUtilErrorCode.MYSQL_CONN_USERPWD_ERROR;
        }

        return DBUtilErrorCode.CONN_DB_ERROR;
    }


    public static DataExchangeException asQueryException(Exception e, String querySql, String table, String userName)
    {
        DBUtilErrorCode dbUtilErrorCode = mySqlQueryErrorAna(e.getMessage());
        if (dbUtilErrorCode == DBUtilErrorCode.MYSQL_QUERY_TABLE_NAME_ERROR && table != null)
        {
            return DataExchangeException.asDataExchangeException(dbUtilErrorCode, "表名为：" + table + " 执行的SQL为:" + querySql + " 具体错误信息为：" + e);
        }
        if (dbUtilErrorCode == DBUtilErrorCode.MYSQL_QUERY_SELECT_PRI_ERROR && userName != null)
        {
            return DataExchangeException.asDataExchangeException(dbUtilErrorCode, "用户名为：" + userName + " 具体错误信息为：" + e);
        }

        return DataExchangeException.asDataExchangeException(dbUtilErrorCode, "执行的SQL为: " + querySql + " 具体错误信息为：" + e);
    }

    public static DBUtilErrorCode mySqlQueryErrorAna(String e)
    {
        if (e.contains(Constant.MYSQL_TABLE_NAME_ERR1) && e.contains(Constant.MYSQL_TABLE_NAME_ERR2))
        {
            return DBUtilErrorCode.MYSQL_QUERY_TABLE_NAME_ERROR;
        } else if (e.contains(Constant.MYSQL_SELECT_PRI))
        {
            return DBUtilErrorCode.MYSQL_QUERY_SELECT_PRI_ERROR;
        } else if (e.contains(Constant.MYSQL_COLUMN1) && e.contains(Constant.MYSQL_COLUMN2))
        {
            return DBUtilErrorCode.MYSQL_QUERY_COLUMN_ERROR;
        } else if (e.contains(Constant.MYSQL_WHERE))
        {
            return DBUtilErrorCode.MYSQL_QUERY_SQL_ERROR;
        }
        return DBUtilErrorCode.READ_RECORD_FAIL;
    }


    public static DataExchangeException asSqlParserException(Exception e, String querySql)
    {
        throw DataExchangeException.asDataExchangeException(DBUtilErrorCode.MYSQL_QUERY_SQL_PARSER_ERROR, "执行的SQL为:" + querySql + " 具体错误信息为：" + e);
    }

    public static DataExchangeException asPreSQLParserException(Exception e, String querySql)
    {
        throw DataExchangeException.asDataExchangeException(DBUtilErrorCode.MYSQL_PRE_SQL_ERROR, "执行的SQL为:" + querySql + " 具体错误信息为：" + e);
    }

    public static DataExchangeException asPostSQLParserException(Exception e, String querySql)
    {
        throw DataExchangeException.asDataExchangeException(DBUtilErrorCode.MYSQL_POST_SQL_ERROR, "执行的SQL为:" + querySql + " 具体错误信息为：" + e);
    }

    public static DataExchangeException asInsertPriException(String userName, String jdbcUrl)
    {
        throw DataExchangeException.asDataExchangeException(DBUtilErrorCode.MYSQL_INSERT_ERROR, "用户名为:" + userName + " jdbcURL为：" + jdbcUrl);
    }

    public static DataExchangeException asDeletePriException(String userName, String jdbcUrl)
    {
        throw DataExchangeException.asDataExchangeException(DBUtilErrorCode.MYSQL_DELETE_ERROR, "用户名为:" + userName + " jdbcURL为：" + jdbcUrl);
    }

    public static DataExchangeException asSplitPKException(Exception e, String splitSql, String splitPkID)
    {

        return DataExchangeException.asDataExchangeException(DBUtilErrorCode.MYSQL_SPLIT_PK_ERROR, "配置的SplitPK为: " + splitPkID + ", 执行的SQL为: " + splitSql + " 具体错误信息为：" + e);
    }
}
